matplotlib

by: Alexli, 7 years ago



I just copied and pasted the code that was posted on the website, but it does not run successfully.
M using python 2.7 with Anaconda Spyder3 environment.
M new to Python , and I tried so many methods to fix it . but none of it works

That will be a great help if anyone can help me to solve this problem. Otherwise I can not continue with rest of following videos :(

import matplotlib.pyplot as plt
import numpy as np
import urllib
import matplotlib.dates as mdates

def bytespdate2num(fmt, encoding='utf-8'):
    strconverter = mdates.strpdate2num(fmt)
    def bytesconverter(b):
        s = b.decode(encoding)
        return strconverter(s)
    return bytesconverter
    

def graph_data(stock):
    # Unfortunately, Yahoo's API is no longer available
    # feel free to adapt the code to another source, or use this drop-in replacement.
    stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement'
    source_code = urllib.request.urlopen(stock_price_url).read().decode()
    stock_data = []
    split_source = source_code.split('n')
    for line in split_source:
        split_line = line.split(',')
        if len(split_line) == 6:
            if 'values' not in line and 'labels' not in line:
                stock_data.append(line)

    date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
                                                          delimiter=',',
                                                          unpack=True,
                                                          # %Y = full year. 2015
                                                          # %y = partial year 15
                                                          # %m = number month
                                                          # %d = number day
                                                          # %H = hours
                                                          # %M = minutes
                                                          # %S = seconds
                                                          # 12-06-2014
                                                          # %m-%d-%Y
                                                          converters={0: bytespdate2num('%Y%m%d')})

    plt.plot_date(date, closep,'-', label='Price')

    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.title('Interesting GraphnCheck it out')
    plt.legend()
    plt.show()


graph_data('TSLA')


runfile('/Users/likekobebyrant/untitled14.py', wdir='/Users/likekobebyrant')
/Users/likekobebyrant/untitled14.py:38: UserWarning: loadtxt: Empty input file: "[]"
  converters={0: bytespdate2num('%Y%m%d')})
Traceback (most recent call last):

  File "<ipython-input-4-1d00813d9fff>", line 1, in <module>
    runfile('/Users/likekobebyrant/untitled14.py', wdir='/Users/likekobebyrant')

  File "/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 93, in execfile
    builtins.execfile(filename, *where)

  File "/Users/likekobebyrant/untitled14.py", line 49, in <module>
    graph_data('TSLA')

  File "/Users/likekobebyrant/untitled14.py", line 38, in graph_data
    converters={0: bytespdate2num('%Y%m%d')})

  File "/anaconda/lib/python2.7/site-packages/numpy/lib/npyio.py", line 1009, in loadtxt
    converters[i] = conv

IndexError: list assignment index out of range



You must be logged in to post. Please login or register an account.